﻿namespace HuaWei.GLY
{
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Base;
    using DevExpress.XtraGrid.Views.Card;
    using DevExpress.XtraTreeList;
    using DevExpress.XtraTreeList.Columns;
    using HuaWei.CommClass;
    using HuaWei.Common;
    using HuaWei.DAI;
    using HuaWei.Windows.Forms;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FrmCT : Form
    {
        private BindingSource bindingSource;
        private BindingSource bindingSource1;
        private SimpleButton btnAddFL;
        private SimpleButton btnChoose;
        private SimpleButton btnDel;
        private SimpleButton btnDelFL;
        private SimpleButton btnLoadImag;
        private CardView cardView1;
        private TreeListColumn colMC;
        private DataSet dataSet = new DataSet();
        private HuaWei.Common.dataSets1 dataSets1;
        private GridColumn gridColumn1;
        private GridControl gridControl1;
        private GroupBox groupBox1;
        private GroupBox groupBox2;
        private IDataAccess idataAccess = null;
        private PanelControl panelControl1;
        private PanelControl panelControl2;
        private PictureEdit pedCT;
        private RepositoryItemImageComboBox repositoryItemImageComboBox1;
        private RepositoryItemImageComboBox repositoryItemImageComboBox2;
        private RepositoryItemPictureEdit repositoryItemPictureEdit1;
        private SimpleButton simpleButton;
        private TreeList tlCTFL;
        private IContainer vroAaJyIa = null;
        private GridColumn 草图名称;

        public FrmCT()
        {
            this.InitializeComponent();
            if (this.idataAccess == null)
            {
                this.idataAccess = DBHelp.IniIDA();
            }
            this.btnChoose.Visible = !JGLX.GLY.Equals(GParams._JGLX);
        }

        private void bindingSource1_PositionChanged(object sender, EventArgs e)
        {
            this.method1();
        }

        private void btnAddFL_Click(object sender, EventArgs e)
        {
            if (this.tlCTFL.FocusedNode != null)
            {
                if (this.tlCTFL.FocusedNode.ParentNode != null)
                {
                    if (this.tlCTFL.FocusedNode.ParentNode == null)
                    {
                        DHelp.DataAddNew(this.bindingSource1, "", "");
                    }
                    else
                    {
                        DHelp.DataAddNewChild(this.bindingSource1, true, "");
                    }
                }
                else
                {
                    MsgBox.ShowMsg("不能新增一级节点，请选择其它节点继续此操作.");
                }
                this.tlCTFL.ShowEditor();
            }
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            if (this.bindingSource.List.Count > 0)
            {
                base.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                MsgBox.ShowMsg("没有可选择的草图，请添加草图后继续此操作!");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if ((this.bindingSource.List.Count > 0) && (MsgBox.ShowQuestion("是否删除当前选中草图？") == System.Windows.Forms.DialogResult.OK))
            {
                DHelp.DelCurRow(this.bindingSource);
                this.method2();
            }
        }

        private void btnDelFL_Click(object sender, EventArgs e)
        {
            if (this.tlCTFL.FocusedNode != null)
            {
                if (this.bindingSource.List.Count > 0)
                {
                    MsgBox.ShowMsg("当前分类包含草图，不能删除。");
                }
                else if (!this.tlCTFL.FocusedNode.HasChildren)
                {
                    DHelp.DelCurRow(this.bindingSource1);
                }
                else
                {
                    MsgBox.ShowMsg("请删除所有子字点后继续此操作。");
                }
            }
        }

        private void btnLoadImag_Click(object sender, EventArgs e)
        {
            if (this.bindingSource1.List.Count > 0)
            {
                string str;
                byte[] buffer = ImageHelp.OpenFile(out str);
                if (!str.Equals(""))
                {
                    string fileName = Path.GetFileName(str);
                    DataRow[] drs=(this.bindingSource.List as DataView).Table.Select("ctlj='" + fileName + "'");
                    if (drs.Length > 0)
                    {
                        MsgBox.ShowMsg("已经存在相同名称的草图，请重新选择!");
                    }
                    else
                    {
                        Stream postedFile = new MemoryStream(buffer);
                        byte[] buffer2 = ImageHelp.ImgToByt(ImageHelp.ZoomAuto(postedFile, 200.0, 200.0, "", ""));
                        byte[] buffer3 = ImageHelp.ImgToByt(ImageHelp.ZoomAuto(postedFile, 800.0, 800.0, "", ""));
                        string objId = DHelp.GetObjId(this.bindingSource1);
                        DataRow row = DHelp.DataAddNew(this.bindingSource, "", "");
                        row.BeginEdit();
                        row["id"] = Guid.NewGuid();
                        if (GParams._JGLX.Equals(JGLX.GLY))
                        {
                            row["BDID"] = -1;
                        }
                        else
                        {
                            row["BDID"] = GParams._BDID;
                        }
                        row["PID"] = objId;
                        row["CT"] = buffer2;
                        row["MAXCT"] = buffer3;
                        row["CTLJ"] = fileName;
                        row.EndEdit();
                        this.method2();
                    }
                }
            }
            else
            {
                MsgBox.ShowMsg("请指定草图分类，再添加草图!");
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.vroAaJyIa != null))
            {
                this.vroAaJyIa.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmCT_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.gridControl1.Focus();
            this.method2();
            Class28.smethod8(this.idataAccess);
        }

        private void FrmCT_Load(object sender, EventArgs e)
        {
            this.lquywmUul();
        }

        public DataRow GetRow()
        {
            return DHelp.GetDataRow(this.bindingSource);
        }

        private void InitializeComponent()
        {
            this.vroAaJyIa = new Container();
            this.gridControl1 = new GridControl();
            this.bindingSource = new BindingSource(this.vroAaJyIa);
            this.cardView1 = new CardView();
            this.草图名称 = new GridColumn();
            this.gridColumn1 = new GridColumn();
            this.repositoryItemPictureEdit1 = new RepositoryItemPictureEdit();
            this.repositoryItemImageComboBox1 = new RepositoryItemImageComboBox();
            this.panelControl1 = new PanelControl();
            this.btnChoose = new SimpleButton();
            this.btnDel = new SimpleButton();
            this.btnLoadImag = new SimpleButton();
            this.pedCT = new PictureEdit();
            this.tlCTFL = new TreeList();
            this.colMC = new TreeListColumn();
            this.bindingSource1 = new BindingSource(this.vroAaJyIa);
            this.dataSets1 = new HuaWei.Common.dataSets1();
            this.repositoryItemImageComboBox2 = new RepositoryItemImageComboBox();
            this.panelControl2 = new PanelControl();
            this.btnDelFL = new SimpleButton();
            this.simpleButton = new SimpleButton();
            this.btnAddFL = new SimpleButton();
            this.groupBox1 = new GroupBox();
            this.groupBox2 = new GroupBox();
            this.gridControl1.BeginInit();
            ((ISupportInitialize) this.bindingSource).BeginInit();
            this.cardView1.BeginInit();
            this.repositoryItemPictureEdit1.BeginInit();
            this.repositoryItemImageComboBox1.BeginInit();
            this.panelControl1.BeginInit();
            this.panelControl1.SuspendLayout();
            this.pedCT.Properties.BeginInit();
            this.tlCTFL.BeginInit();
            ((ISupportInitialize) this.bindingSource1).BeginInit();
            this.dataSets1.BeginInit();
            this.repositoryItemImageComboBox2.BeginInit();
            this.panelControl2.BeginInit();
            this.panelControl2.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            base.SuspendLayout();
            this.gridControl1.DataMember = null;
            this.gridControl1.DataSource = this.bindingSource;
            this.gridControl1.Dock = DockStyle.Left;
            this.gridControl1.Location = new Point(3, 0x2e);
            this.gridControl1.MainView = this.cardView1;
            this.gridControl1.Name = "gridControl1";
            this.gridControl1.RepositoryItems.AddRange(new RepositoryItem[] { this.repositoryItemPictureEdit1, this.repositoryItemImageComboBox1 });
            this.gridControl1.Size = new Size(0xbd, 0x206);
            this.gridControl1.TabIndex = 1;
            this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] { this.cardView1 });
            this.bindingSource.DataMember = "GCLCT";
            this.bindingSource.DataSource = this.dataSets1;
            this.cardView1.CardInterval = 4;
            this.cardView1.CardWidth = 100;
            this.cardView1.Columns.AddRange(new GridColumn[] { this.草图名称, this.gridColumn1 });
            this.cardView1.FocusedCardTopFieldIndex = 0;
            this.cardView1.GridControl = this.gridControl1;
            this.cardView1.MaximumCardColumns = 3;
            this.cardView1.MaximumCardRows = 3;
            this.cardView1.Name = "cardView1";
            this.cardView1.OptionsBehavior.AutoHorzWidth = true;
            this.cardView1.OptionsBehavior.FieldAutoHeight = true;
            this.cardView1.OptionsView.ShowCardCaption = false;
            this.cardView1.OptionsView.ShowFieldCaptions = false;
            this.cardView1.OptionsView.ShowLines = false;
            this.cardView1.OptionsView.ShowQuickCustomizeButton = false;
            this.cardView1.VertScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Auto;
            this.草图名称.Caption = "gridColumn1";
            this.草图名称.FieldName = "CTLJ";
            this.草图名称.Name = "草图名称";
            this.草图名称.Visible = true;
            this.草图名称.VisibleIndex = 0;
            this.gridColumn1.Caption = "gridColumn1";
            this.gridColumn1.ColumnEdit = this.repositoryItemPictureEdit1;
            this.gridColumn1.FieldName = "CT";
            this.gridColumn1.Name = "gridColumn1";
            this.gridColumn1.Visible = true;
            this.gridColumn1.VisibleIndex = 1;
            this.repositoryItemPictureEdit1.Name = "repositoryItemPictureEdit1";
            this.repositoryItemPictureEdit1.SizeMode = PictureSizeMode.Stretch;
            this.repositoryItemImageComboBox1.AutoHeight = false;
            this.repositoryItemImageComboBox1.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repositoryItemImageComboBox1.Name = "repositoryItemImageComboBox1";
            this.panelControl1.Controls.Add(this.btnChoose);
            this.panelControl1.Controls.Add(this.btnDel);
            this.panelControl1.Controls.Add(this.btnLoadImag);
            this.panelControl1.Dock = DockStyle.Top;
            this.panelControl1.Location = new Point(3, 0x11);
            this.panelControl1.Name = "panelControl1";
            this.panelControl1.Size = new Size(0x264, 0x1d);
            this.panelControl1.TabIndex = 12;
            this.btnChoose.Dock = DockStyle.Left;
            this.btnChoose.Location = new Point(0x76, 2);
            this.btnChoose.Name = "btnChoose";
            this.btnChoose.Size = new Size(0x3a, 0x19);
            this.btnChoose.TabIndex = 6;
            this.btnChoose.Tag = "-1";
            this.btnChoose.Text = "选择";
            this.btnChoose.Click += new EventHandler(this.btnChoose_Click);
            this.btnDel.Dock = DockStyle.Left;
            this.btnDel.Location = new Point(60, 2);
            this.btnDel.Name = "btnDel";
            this.btnDel.Size = new Size(0x3a, 0x19);
            this.btnDel.TabIndex = 4;
            this.btnDel.Tag = "-1";
            this.btnDel.Text = "删除";
            this.btnDel.Click += new EventHandler(this.btnDel_Click);
            this.btnLoadImag.Dock = DockStyle.Left;
            this.btnLoadImag.Location = new Point(2, 2);
            this.btnLoadImag.Name = "btnLoadImag";
            this.btnLoadImag.Size = new Size(0x3a, 0x19);
            this.btnLoadImag.TabIndex = 5;
            this.btnLoadImag.Tag = "-1";
            this.btnLoadImag.Text = "添加";
            this.btnLoadImag.Click += new EventHandler(this.btnLoadImag_Click);
            this.pedCT.DataBindings.Add(new Binding("EditValue", this.bindingSource, "MAXCT", true));
            this.pedCT.Dock = DockStyle.Fill;
            this.pedCT.Location = new Point(0xc0, 0x2e);
            this.pedCT.Name = "pedCT";
            this.pedCT.Properties.SizeMode = PictureSizeMode.Zoom;
            this.pedCT.Size = new Size(0x1a7, 0x206);
            this.pedCT.TabIndex = 13;
            this.tlCTFL.Columns.AddRange(new TreeListColumn[] { this.colMC });
            this.tlCTFL.DataSource = this.bindingSource1;
            this.tlCTFL.Dock = DockStyle.Fill;
            this.tlCTFL.ImageIndexFieldName = "";
            this.tlCTFL.Location = new Point(3, 0x2c);
            this.tlCTFL.Name = "tlCTFL";
            this.tlCTFL.OptionsView.ShowColumns = false;
            this.tlCTFL.ParentFieldName = "PID";
            this.tlCTFL.PreviewFieldName = "MC";
            this.tlCTFL.RepositoryItems.AddRange(new RepositoryItem[] { this.repositoryItemImageComboBox2 });
            this.tlCTFL.Size = new Size(0xc2, 520);
            this.tlCTFL.TabIndex = 14;
            this.tlCTFL.TreeLineStyle = LineStyle.None;
            this.colMC.FieldName = "MC";
            this.colMC.Name = "colMC";
            this.colMC.Visible = true;
            this.colMC.VisibleIndex = 0;
            this.colMC.Width = 0x60;
            this.bindingSource1.DataMember = "GCLCT";
            this.bindingSource1.DataSource = this.dataSets1;
            this.dataSets1.DataSetName = "dataSets1";
            this.dataSets1.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
            this.repositoryItemImageComboBox2.AutoHeight = false;
            this.repositoryItemImageComboBox2.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repositoryItemImageComboBox2.Items.AddRange(new ImageComboBoxItem[] { new ImageComboBoxItem("", "1", 1), new ImageComboBoxItem("", "2", 5), new ImageComboBoxItem("", "3", 11) });
            this.repositoryItemImageComboBox2.Name = "repositoryItemImageComboBox2";
            this.panelControl2.Controls.Add(this.btnDelFL);
            this.panelControl2.Controls.Add(this.simpleButton);
            this.panelControl2.Controls.Add(this.btnAddFL);
            this.panelControl2.Dock = DockStyle.Top;
            this.panelControl2.Location = new Point(3, 0x11);
            this.panelControl2.Name = "panelControl2";
            this.panelControl2.Size = new Size(0xc2, 0x1b);
            this.panelControl2.TabIndex = 15;
            this.btnDelFL.Dock = DockStyle.Left;
            this.btnDelFL.Location = new Point(0x7a, 2);
            this.btnDelFL.Name = "btnDelFL";
            this.btnDelFL.Size = new Size(0x2f, 0x17);
            this.btnDelFL.TabIndex = 2;
            this.btnDelFL.Text = "删除";
            this.btnDelFL.Click += new EventHandler(this.btnDelFL_Click);
            this.simpleButton.Dock = DockStyle.Left;
            this.simpleButton.Location = new Point(0x3e, 2);
            this.simpleButton.Name = "btnAddXJFL";
            this.simpleButton.Size = new Size(60, 0x17);
            this.simpleButton.TabIndex = 1;
            this.simpleButton.Text = "新增下级";
            this.simpleButton.Click += new EventHandler(this.simpleButton_Click);
            this.btnAddFL.Dock = DockStyle.Left;
            this.btnAddFL.Location = new Point(2, 2);
            this.btnAddFL.Name = "btnAddFL";
            this.btnAddFL.Size = new Size(60, 0x17);
            this.btnAddFL.TabIndex = 0;
            this.btnAddFL.Text = "新增同级";
            this.btnAddFL.Click += new EventHandler(this.btnAddFL_Click);
            this.groupBox1.Controls.Add(this.tlCTFL);
            this.groupBox1.Controls.Add(this.panelControl2);
            this.groupBox1.Dock = DockStyle.Left;
            this.groupBox1.Location = new Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(200, 0x237);
            this.groupBox1.TabIndex = 0x10;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "草图分类";
            this.groupBox2.Controls.Add(this.pedCT);
            this.groupBox2.Controls.Add(this.gridControl1);
            this.groupBox2.Controls.Add(this.panelControl1);
            this.groupBox2.Dock = DockStyle.Fill;
            this.groupBox2.Location = new Point(200, 0);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new Size(0x26a, 0x237);
            this.groupBox2.TabIndex = 0x11;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "草图";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            base.ClientSize = new Size(0x332, 0x237);
            base.Controls.Add(this.groupBox2);
            base.Controls.Add(this.groupBox1);
            base.Name = "FrmCT";
            base.StartPosition = FormStartPosition.CenterParent;
            this.Text = "草图维护";
            base.FormClosed += new FormClosedEventHandler(this.FrmCT_FormClosed);
            base.Load += new EventHandler(this.FrmCT_Load);
            this.gridControl1.EndInit();
            ((ISupportInitialize) this.bindingSource).EndInit();
            this.cardView1.EndInit();
            this.repositoryItemPictureEdit1.EndInit();
            this.repositoryItemImageComboBox1.EndInit();
            this.panelControl1.EndInit();
            this.panelControl1.ResumeLayout(false);
            this.pedCT.Properties.EndInit();
            this.tlCTFL.EndInit();
            ((ISupportInitialize) this.bindingSource1).EndInit();
            this.dataSets1.EndInit();
            this.repositoryItemImageComboBox2.EndInit();
            this.panelControl2.EndInit();
            this.panelControl2.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void lquywmUul()
        {
            DataRow row;
            this.dataSet.Merge(this.idataAccess.ExecuteDataSet("select * from CTFL", CommandType.Text, "CTFL"));
            this.dataSet.Merge(this.idataAccess.ExecuteDataSet("select * from GCLCT", CommandType.Text, "GCLCT"));
            DataTable table = this.dataSet.Tables["CTFL"];
            this.bindingSource.DataSource = this.dataSet.Tables["GCLCT"];
            this.bindingSource1.DataSource = table;
            this.bindingSource1.PositionChanged += new EventHandler(this.bindingSource1_PositionChanged);
            this.dataSet.Tables["CTFL"].TableNewRow += new DataTableNewRowEventHandler(this.method);
            if (GParams._JGLX.Equals(JGLX.GLY))
            {
                if (table.Rows.Count == 0)
                {
                    row = table.NewRow();
                    row["mc"] = "通用草图";
                    row.EndEdit();
                    table.Rows.Add(row);
                }
            }
            else if (table.Select(string.Format("bdid='{0}'", GParams._BDID)).Length == 0)
            {
                row = table.NewRow();
                row["mc"] = GParams._BDName;
                row.EndEdit();
                table.Rows.Add(row);
            }
            this.method1();
        }

        private void method(object sender, DataTableNewRowEventArgs e)
        {
            DataColumnCollection columns = e.Row.Table.Columns;
            e.Row["id"] = Guid.NewGuid();
            if (GParams._JGLX.Equals(JGLX.GLY))
            {
                e.Row["BDID"] = -1;
                e.Row["PID"] = -1;
            }
            else
            {
                e.Row["BDID"] = GParams._BDID;
                e.Row["PID"] = -1;
            }
        }

        private void method1()
        {
            string str = DHelp.GetObjValue(this.bindingSource1, "ID", false);
            DHelp.GetObjValue(this.bindingSource1, "PID", false);
            this.bindingSource.Filter = "PID='" + str + "'";
            DataRow dataRow = DHelp.GetDataRow(this.bindingSource1);
            if (!JGLX.GLY.Equals(GParams._JGLX))
            {
                if (!(dataRow["bdid"].Equals("-1") || dataRow["pid"].Equals("-1")))
                {
                    this.colMC.OptionsColumn.AllowEdit = true;
                    this.btnDelFL.Enabled = true;
                }
                else
                {
                    this.colMC.OptionsColumn.AllowEdit = false;
                    this.btnDelFL.Enabled = false;
                }
                if (!dataRow["bdid"].Equals("-1"))
                {
                    this.btnLoadImag.Enabled = true;
                    this.btnDel.Enabled = true;
                    this.simpleButton.Enabled = true;
                    this.btnAddFL.Enabled = true;
                }
                else
                {
                    this.btnLoadImag.Enabled = false;
                    this.btnDel.Enabled = false;
                    this.simpleButton.Enabled = false;
                    this.btnAddFL.Enabled = false;
                }
            }
            else
            {
                bool flag = !dataRow["pid"].Equals("-1");
                this.colMC.OptionsColumn.AllowEdit = flag;
                this.btnDelFL.Enabled = flag;
            }
        }

        private void method2()
        {
            this.bindingSource1.EndEdit();
            this.bindingSource.EndEdit();
            if (this.dataSet.HasChanges())
            {
                this.idataAccess.OpenDatabase();
                this.idataAccess.BeginTrans();
                try
                {
                    this.idataAccess.Update(this.dataSet, true);
                    this.idataAccess.Commit();
                }
                finally
                {
                    this.idataAccess.CloseDatabase();
                }
            }
        }

        private void method3(object sender, EventArgs e)
        {
        }

        private void method4(object sender, EventArgs e)
        {
            DHelp.DataAddNew(this.bindingSource, "", "");
        }

        private void simpleButton_Click(object sender, EventArgs e)
        {
            DHelp.DataAddNewChild(this.bindingSource1, false, "");
            this.tlCTFL.ShowEditor();
        }
    }
}

